<template>
  <div class="app-container">
    <el-dialog title="新增配置" :visible.sync="visible" width="800px" top="5vh" append-to-body>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="是否启用" prop="configState">
          <el-radio-group v-model="ruleForm.configState">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="配置名称" prop="configName">
          <el-input v-model="ruleForm.configName" placeholder="请输入配置名称"></el-input>
        </el-form-item>
        <el-form-item label="数据库名" prop="dataBase">
          <el-input v-model="ruleForm.dataBase" placeholder="请输入数据库名"></el-input>
        </el-form-item>

        <el-form-item label="默认包路径" prop="packageName">
          <el-input v-model="ruleForm.packageName" placeholder="请输入默认包路径"></el-input>
        </el-form-item>
        <el-form-item label="去除表前缀" prop="autoRemovePre">
          <el-radio-group v-model="ruleForm.autoRemovePre">
            <el-radio label="0">去除</el-radio>
            <el-radio label="1">不去除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="表前缀" prop="tablePrefix">
          <el-input v-model="ruleForm.tablePrefix"
                    placeholder="请输入表前缀（生成类名不会包含表前缀，多个用逗号分隔）"></el-input>
        </el-form-item>
        <el-form-item label="忽略字段" prop="ignoreColumn">
          <el-input v-model="ruleForm.ignoreColumn"
                    placeholder="请输入忽略字段（多个用逗号分隔，默认忽略create_by，create_time，update_by，update_time）"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="ruleForm.author" placeholder="请输入作者"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="configRemark">
          <el-input v-model="ruleForm.configRemark" placeholder="请输入备注"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import genApi from "@/api/gen";

export default {
  name: "addConfigFrom",
  data() {
    return {
      visible: false,
      ruleForm: {
        configName: '',
        author: '',
        packageName: '',
        autoRemovePre: '0',
        tablePrefix: '',
        configState: '1',
        dataBase: '',
        configRemark: '',
        ignoreColumn: ''
      },
      rules: {
        configName: [{required: true, message: '请输入配置名称', trigger: 'blur'},
          {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}],
        packageName: [{required: true, message: '请输入默认包路径', trigger: 'blur'}],
        author: [{required: true, message: '请输入作者', trigger: 'blur'}],
        dataBase: [{required: true, message: '请输入数据库名', trigger: 'blur'}],
        configRemark: [{required: true, message: '请输入备注', trigger: 'blur'}],
        resource: [{required: true, message: '请选择活动资源', trigger: 'change'}],
        autoRemovePre: [{required: true, message: '请选择是否去除表前缀', trigger: 'change'}],
        configState: [{required: true, message: '请选择是否启动', trigger: 'change'}],
      }
    }
  },
  created() {
  },
  methods: {
    // 显示弹框
    show(data) {
      if (data === undefined) {
        this.resetForm()
      } else {
        this.ruleForm = data
      }
      this.visible = true

    },
    // 提交表单
    submitForm() {
      if (this.ruleForm.configId) {
        genApi.updateConfig(this.ruleForm).then(response => {
          if (response.code === 200) {
            this.visible = false
            this.resetForm()
            this.$emit('ok')
          }else{
            this.$message.error("修改失败 " + response.msg)
          }
        })
      } else {
        genApi.addConfig(this.ruleForm).then(response => {
          if (response.code === 200) {
            this.visible = false
            this.resetForm()
            this.$emit('ok')
          } else {
            this.$message.error("添加失败 " + response.msg)
          }
        })
      }

    },
    // 重置表单
    resetForm() {
      this.ruleForm = {
        configName: '',
        author: '',
        packageName: '',
        autoRemovePre: '0',
        tablePrefix: '',
        configState: '1',
        dataBase: '',
        remark: '',
        ignoreColumn: ''
      }
    }
  }
}
</script>

